$(function(){
    let id = location.search.split('?')[1]
    if(id){
        $.get('http://localhost:8000/product',data=>{
            let newData = data.filter(item=>{
                return item.id == id
            })
            $('[name=id]').val(newData[0].id)
            $('[name=name]').val(newData[0].name)
            $('[name=price]').val(newData[0].price)
        })
    }
})

function save(){
    obj={
        id:$('[name=id]').val(),
        name:$('[name=name]').val(),
        price:$('[name=price]').val()
    }
    if(obj.id){
        putProduct(obj.id,obj).then(data=>{
            if(data.code===1000){
                location.href = './index.html'
            }else{
                alert(code.msg)
            }
        })
    }else{
        postProduct(obj).then(data=>{
            if(data.code===1000){
                location.href = './index.html'
            }else{
                alert(code.msg)
            }
        })
    }
}

function cancel(){
    location.href='./index.html'
}